<template>
  <div class="app">
    <headerVue></headerVue>
    <div class="nav">
      <ul>
        <li>
          <router-link to="/goods">商品</router-link>
        </li>
        <li>
          <router-link to="/rating">评价</router-link>
        </li>
        <li>
          <router-link to="/seller">商家</router-link>
        </li>
      </ul>
      <router-view></router-view>
    </div>
    <div class="bottom">
      <div>
        <div ref="cart" class="cart iconfont icon-gouwuche" @click="isShowShopCar(),    changeCart()">
        </div>
        <p class="money">￥{{      getSumPrice      }}</p>
      </div>
      <div>另需配送费￥4元</div>
      <div>￥20起送</div>
    </div>
    <div class="shopCarList" v-show="$store.state.shopCarIsShow">
      <div class="mask" @click="isShowShopCar(),changeCart()"></div>
      <div class="shopCarInfo">
        <p class="carTitle">
          <span>购物车</span>
          <span @click="clearCart">清空</span>
        </p>
        <ul>
          <li v-for="item in shopCarList" :key="item.name">
            <p>{{ item.name }}</p>
            <div class="btns">
              <span class="onePrice">￥{{      item.price      }}</span>
              <span @click="reduceCount(item)" class="jian">-</span>
              <span class="count">{{      item.count      }}</span>
              <span @click="addCount(item)" class="jia">+</span>
            </div>
          </li>
        </ul>
      </div>
    </div>
  </div>
</template>

<script>
import { mapState, mapMutations } from 'vuex';
import headerVue from './PageViews/header.vue';
export default {
  data() {
    return {
      clickCount: 0
    }
  },
  components: {
    headerVue,
  },
  computed: {
    ...mapState(["shopCarList"]),
    getSumPrice() {
      let num = 0;
      this.shopCarList.forEach((item) => {
        num += item.price * item.count;
      });
      return num;
    },
  },
  methods: {
    addCount(food) {
      if (!food.count) {
        food.count = 1;
      } else {
        food.count++;
      }
      this.$store.commit("setShopCarList", {
        item: food,
      })
    },
    reduceCount(food) {
      food.count--;
      const index = this.shopCarList.findIndex((item) => {
        return item.count === 0;
      });
      if (index !== -1) {
        this.shopCarList.splice(index, 1);
      }
    },
    isShowShopCar() {
      this.$store.commit("shopCarIsShow");
    },
    changeCart() {
      this.clickCount++;
      if (this.clickCount > 0 && this.clickCount % 2 !== 0) {
        this.$refs.cart.style.background = "rgb(0,160,220)";
        this.$refs.cart.style.color = 'white';
      } else {
        this.$refs.cart.style.background = "";
        this.$refs.cart.style.color = '';
      }
    },
    clearCart() {
      this.$store.commit('clearShopCar');
    }
  }
}

</script>

<style scoped>
.app {
  height: 100%;
  position: relative;
}

::-webkit-scrollbar {
  display: none;
}

.shopCarList {
  position: absolute;
  left: 0;
  top: 0;
  width: 100%;
  height: 93%;
}

.shopCarList>.mask {
  width: 100%;
  height: 60%;
  background: rgba(7, 17, 27, 0.6);
}

.shopCarList>.shopCarInfo {
  height: 300px;
  background: pink;
}

.shopCarList>.shopCarInfo>ul {
  height: 204px;
  background-color: white;
  padding: 0px 18px;
  overflow: auto;
}

.shopCarList>.shopCarInfo>ul>li {
  position: relative;
  display: flex;
  height: 48px;
  border-bottom: 1px solid rgba(7, 17, 27, 0.1);
  justify-content: space-between;
  align-items: center;
  font-size: 14px;
  color: rgb(7, 17, 27);

}

.count {
  margin: 0px 8px;
  color: rgb(147, 153, 159);
}

a {
  font-size: 14px;
  color: rgb(77, 85, 93);
}

a:active {
  color: rgb(240, 20, 20);
}

.nav {
  height: 73%;
  overflow: hidden;
}

.nav>ul {
  display: flex;
  justify-content: space-around;
  border-bottom: 1px solid rgba(7, 17, 27, 0.1);
}

.nav>ul>li {
  height: 40px;
  line-height: 40px;
}

.bottom {
  height: 50px;
  background: #141d27;
  display: flex;
  justify-content: space-between;
  align-items: center;
  color: gray;
  position: relative;
  z-index: 2;
}

.bottom>div {
  display: flex;
  flex: 1;
  width: 210px;
  height: 26px;
  text-align: center;
  line-height: 26px;
  justify-content: center;
  font-size: 16px;
  padding: 12px 0px;
}

.bottom>div:nth-child(1)>p {
  margin-left: 70px;
  font-size: 16px;
  font-weight: 700;
}

.bottom>div:nth-child(1) {
  border-right: 1px solid rgb(255, 255, 255, 0.1);
}

.bottom>div:nth-child(2) {
  padding-left: 12px;
  font-size: 14px;
  color: rgb(255, 255, 255, 0.4);
  line-height: 24px;
}

.bottom>div:nth-child(3) {
  padding-left: 12px;
  font-size: 14px;
  color: rgb(255, 255, 255, 0.4);
  font-weight: 700;
  line-height: 24px;
  background-color: rgb(255, 255, 255, 0.4);
}

.cart {
  display: inline-block;
  width: 34px;
  height: 34px;
  border: 9px solid #141d27;
  border-radius: 50%;
  position: absolute;
  background-color: rgb(255, 255, 255, 0.4);
  top: -9px;
  left: 18px;
  font-size: 24px;
  line-height: 37px;
  color: rgb(255, 255, 255, 0.4);
}

.carTitle {
  height: 45px;
  padding: 0px 18px;
  display: flex;
  justify-content: space-between;
  background-color: #f3f5f7;
  border-bottom: 1px solid rgba(7, 17, 27, 0.1);
}

.carTitle>span {
  font-size: 14px;
  font-weight: 200;
  color: rgb(7, 17, 27);
  line-height: 40px;
}

.carTitle>span:nth-child(2) {
  font-size: 12px;
  color: rgb(0, 160, 220);
}

.btns {
  position: absolute;
  right: 0px;
  bottom: 12px;
}

.jian {
  display: inline-block;
  width: 20px;
  height: 20px;
  background: blue;
  text-align: center;
  line-height: 19px;
  border-radius: 50%;
  text-align: center;
  font-size: 16px;
  font-weight: bold;
  color: rgb(0, 160, 220);
  background-color: white;
  border: 2px solid rgb(0, 160, 220);
}

.jia {
  display: inline-block;
  width: 20px;
  height: 20px;
  background: blue;
  text-align: center;
  line-height: 19px;
  border-radius: 50%;
  text-align: center;
  font-size: 16px;
  font-weight: bold;
  color: white;
  background-color: rgb(0, 160, 220);
  border: 2px solid rgb(0, 160, 220);
}

.onePrice {
  margin-right: 10px;
  font-size: 14px;
  color: rgb(240, 20, 20);
}
</style>